﻿#include "fightPage.h"
#include <iostream>
using namespace std;
bool random(int i);
void fightPage::loadPage() {

}
void fightPage::drawPage(Hero& hero) {
	system("cls");
	//cout << "欢迎来到" << name << endl;
	cout << "姓名：" << enemy->get_name() << endl;
	cout << "血量： " << enemy->get_hp() << endl;
	cout << "攻击力:  " << enemy->get_atk() << endl << endl;
	cout << "您的姓名：" << hero.get_name() << endl;
	cout << "您的剩余血量： " << hero.get_hp() << "/" << hero.get_maxhp() << endl;
	cout << "法力值:  " << hero.get_mp() << "/" << hero.get_maxmp() << endl;
	cout << "防御:  " << hero.getDf() << endl;
	
}
void fightPage::updatePage(Hero& hero) {
	static double tmpAtk=0;
	static double fire = 0;
	cout << "攻击力:  " << hero.get_atk() + hero.getAddAtk()+tmpAtk << endl;
	cout << "1.普通攻击  2.使用技能  3.使用药品  4.逃跑" << endl;
	int in;
	cin >> ws;
	if(cin >> in){
		switch (in) {
		case 1: {
			enemy->add_hp(-(hero.get_atk() + hero.getAddAtk() + tmpAtk));
			cout << "您对" << enemy->get_name() << "造成了" << hero.get_atk() + hero.getAddAtk() + tmpAtk << "点伤害" << endl;
			if (fire > 0) {
				enemy->add_hp(-fire);
				cout << "灼烧了敌人" << fire << "点伤害" << endl;
			}
			if (enemy->get_hp() <= 0) {
				cout << "敌人死亡" << endl;
				system("pause");
			}
			else {
				double x = (enemy->get_atk() - hero.getDf()) < 0 ? 0 : enemy->get_atk() - hero.getDf();
				hero.add_hp(-x);
				cout << enemy->get_name() << "对您" << "造成了" << x << "点伤害" << endl;
				system("pause");
			}
			break;
		}
		case 2: {
			if (hero.get_skill(0) + hero.get_skill(1) + hero.get_skill(2) == 0) {
				cout << "您暂时没有技能" << endl;
				system("pause");
			}
			else {
				int choose;
				hero.showSkill();
				cout << "请选择要使用的技能" << endl;
				cin >> choose;
				if (choose == 1) {
					double x = hero.get_atk() + hero.getAddAtk() + tmpAtk;
					x = x * 0.1 * hero.get_skill(0);
					hero.add_mp(-(hero.get_skill(0) * 10));
					fire += x;
					if (fire > 0) {
						enemy->add_hp(-fire);
						cout << "灼烧了敌人" << fire << "点伤害" << endl;
					}
					if (enemy->get_hp() <= 0) {
						cout << "敌人死亡" << endl;
						system("pause");
					}
					else {
						double x = (enemy->get_atk() - hero.getDf()) < 0 ? 0 : enemy->get_atk() - hero.getDf();
						hero.add_hp(-x);
						cout << enemy->get_name() << "对您" << "造成了" << x << "点伤害" << endl;
						system("pause");
					}
				}
				else if (choose == 2) {
					double x = hero.get_atk() + hero.getAddAtk() + tmpAtk;
					enemy->add_hp(-(x));
					hero.add_mp(-(hero.get_skill(1) * 10));
					cout << "您对" << enemy->get_name() << "造成了" << x << "点伤害" << endl;
					if (random(hero.get_skill(1) * 10)) {
						cout << "成功冰冻敌人" << endl;
						if (fire > 0) {
							enemy->add_hp(-fire);
							cout << "灼烧了敌人" << fire << "点伤害" << endl;
						}
						if (enemy->get_hp() <= 0) {
							cout << "敌人死亡" << endl;
							//system("pause");
						}
						system("pause");
					}
					else {
						cout << "冰冻敌人失败" << endl;
						if (fire > 0) {
							enemy->add_hp(-fire);
							cout << "灼烧了敌人" << fire << "点伤害" << endl;
						}
						if (enemy->get_hp() <= 0) {
							cout << "敌人死亡" << endl;
							system("pause");
						}
						else {
							double x = (enemy->get_atk() - hero.getDf()) < 0 ? 0 : enemy->get_atk() - hero.getDf();
							hero.add_hp(-x);
							cout << enemy->get_name() << "对您" << "造成了" << x << "点伤害" << endl;
							system("pause");
						}
					}

				}
				else if (choose == 3) {
					hero.add_mp(-(hero.get_skill(2) * 10));
					double x = hero.get_atk() + hero.getAddAtk() + tmpAtk;
					x = x * 0.75;
					enemy->add_hp(-(x));
					cout << "您对" << enemy->get_name() << "造成了" << x << "点伤害" << endl;
					hero.add_hp(x * hero.get_skill(2) * 0.1);
					cout << "回复了" << x * hero.get_skill(2) * 0.1 << "点血量" << endl;
					if (fire > 0) {
						enemy->add_hp(-fire);
						cout << "灼烧了敌人" << fire << "点伤害" << endl;
					}
					if (enemy->get_hp() <= 0) {
						cout << "敌人死亡" << endl;
						system("pause");
					}
					else {
						double x = (enemy->get_atk() - hero.getDf()) < 0 ? 0 : enemy->get_atk() - hero.getDf();
						hero.add_hp(-x);
						cout << enemy->get_name() << "对您" << "造成了" << x << "点伤害" << endl;
						system("pause");
					}
				}
				else {
					cout << "非法的输入" << endl;
					system("pause");
				}
			}
			break;
		}
		case 3: {
			hero.showDrug();
			cout << "请输入要使用的药品" << endl;
			int choose;
			cin >> choose;
			if (choose < 10 || choose >15) {
				cout << "非法的输入！" << endl;
				system("pause");
			}
			else {
				if (hero.getGoodsNum(choose) == 0) {
					cout << "并未拥有此药品" << endl;
					system("pause");
				}
				else {
					if (choose == 13) {
						tmpAtk += drug[choose - 10].getaddatk_t();
					}
					else {
						hero.useDrug(choose);
						hero.add_Goods(choose, -1);
					}
				}
			}
			break;
		}
		case 4: {
			isQuit = true;
			break;
		}
		default: {
			cout << "非法的输入！" << endl;
			system("pause");
		}
		}
	}
	else {
	cin.clear();
	char ch;
	cin.get(ch);
	const char* cinError = "输入错误，请重新输入";
	COLOR_PRINT(cinError, 4);
	}
}
bool fightPage::isquitPage(Hero& hero) {
	if (hero.get_hp() <= 0) {
		cout << "您死了,期待你的下次重生" << endl;
		exit(0);
		isQuit = true;
	}
	else if(enemy->get_hp()<=0) {
		hero.add_xp(enemy->get_id() * 10);
		cout << "获得" << enemy->get_id() * 10 << "点经验" << endl;
		if (enemy->get_id() == 2) {
			if (hero.nowTask() == 1) {
				hero.finishTask();
			}
		}
		else {
			if (hero.nowTask() == enemy->get_id()) {
				hero.finishTask();
				if (hero.nowTask() == 6) {
					system("cls");
					string s = "在你（南宫离的转世）的努力下，神隐之地重回安宁，世间又多了一个新的传奇，这片土地的故事还在继续.....";
					for (int i = 0;i < s.length();i++) {
						cout << s[i];
						if (!_kbhit()) {
							Sleep(25);
						}
					}
				}
			}
		}
		isQuit = true;
	}
	return isQuit;
}
int fightPage::get_position()const {
	return 0;
}
bool random(int i) {
	int x = rand() % 100;
	return x < i;
}